<?php

/**
 * Created by PhpStorm.
 * User: fujianguo
 * Date: 2017/6/23
 * Time: 下午12:39
 */
class ActivityBase extends CommonModel
{
    public function __get($name)
    {
        $getter = 'get' . $this->toCamelCase($name);

        if (method_exists($this, $getter)) {
            return $this->$getter();
        } elseif (method_exists($this, 'set' . $name)) {
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
        } else {
            return parent::__get($name);
        }
    }

    /**
     * Sets value of an object property.
     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `$object->property = $value;`.
     * @param string $name the property name or the event name
     * @param mixed $value the property value
     * @throws UnknownPropertyException if the property is not defined
     * @throws InvalidCallException if the property is read-only
     * @see __get()
     */
    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } elseif (method_exists($this, 'get' . $name)) {
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
        } else {
            parent::__set($name,$value);
        }
    }

//    /**
//     * Checks if a property is set, i.e. defined and not null.
//     *
//     * Do not call this method directly as it is a PHP magic method that
//     * will be implicitly called when executing `isset($object->property)`.
//     *
//     * Note that if the property is not defined, false will be returned.
//     * @param string $name the property name or the event name
//     * @return bool whether the named property is set (not null).
//     * @see http://php.net/manual/en/function.isset.php
//     */
//    public function __isset($name)
//    {
//        $getter = 'get' . $name;
//        if (method_exists($this, $getter)) {
//            return $this->$getter() !== null;
//        } else {
//            return false;
//        }
//    }
//
//    /**
//     * Sets an object property to null.
//     *
//     * Do not call this method directly as it is a PHP magic method that
//     * will be implicitly called when executing `unset($object->property)`.
//     *
//     * Note that if the property is not defined, this method will do nothing.
//     * If the property is read-only, it will throw an exception.
//     * @param string $name the property name
//     * @throws InvalidCallException if the property is read only.
//     * @see http://php.net/manual/en/function.unset.php
//     */
//    public function __unset($name)
//    {
//        $setter = 'set' . $name;
//        if (method_exists($this, $setter)) {
//            $this->$setter(null);
//        } elseif (method_exists($this, 'get' . $name)) {
//            throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
//        }
//    }
//
//    /**
//     * Calls the named method which is not a class method.
//     *
//     * Do not call this method directly as it is a PHP magic method that
//     * will be implicitly called when an unknown method is being invoked.
//     * @param string $name the method name
//     * @param array $params method parameters
//     * @throws UnknownMethodException when calling unknown method
//     * @return mixed the method return value
//     */
//    public function __call($name, $params)
//    {
//        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
//    }
//
//    /**
//     * Returns a value indicating whether a property is defined.
//     * A property is defined if:
//     *
//     * - the class has a getter or setter method associated with the specified name
//     *   (in this case, property name is case-insensitive);
//     * - the class has a member variable with the specified name (when `$checkVars` is true);
//     *
//     * @param string $name the property name
//     * @param bool $checkVars whether to treat member variables as properties
//     * @return bool whether the property is defined
//     * @see canGetProperty()
//     * @see canSetProperty()
//     */
//    public function hasProperty($name, $checkVars = true)
//    {
//        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
//    }
//
//    /**
//     * Returns a value indicating whether a property can be read.
//     * A property is readable if:
//     *
//     * - the class has a getter method associated with the specified name
//     *   (in this case, property name is case-insensitive);
//     * - the class has a member variable with the specified name (when `$checkVars` is true);
//     *
//     * @param string $name the property name
//     * @param bool $checkVars whether to treat member variables as properties
//     * @return bool whether the property can be read
//     * @see canSetProperty()
//     */
//    public function canGetProperty($name, $checkVars = true)
//    {
//        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
//    }
//
//    /**
//     * Returns a value indicating whether a property can be set.
//     * A property is writable if:
//     *
//     * - the class has a setter method associated with the specified name
//     *   (in this case, property name is case-insensitive);
//     * - the class has a member variable with the specified name (when `$checkVars` is true);
//     *
//     * @param string $name the property name
//     * @param bool $checkVars whether to treat member variables as properties
//     * @return bool whether the property can be written
//     * @see canGetProperty()
//     */
//    public function canSetProperty($name, $checkVars = true)
//    {
//        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
//    }
//
//    /**
//     * Returns a value indicating whether a method is defined.
//     *
//     * The default implementation is a call to php function `method_exists()`.
//     * You may override this method when you implemented the php magic method `__call()`.
//     * @param string $name the method name
//     * @return bool whether the method is defined
//     */
//    public function hasMethod($name)
//    {
//        return method_exists($this, $name);
//    }

    public function save($data = '', $options = array())
    {
        $result = false;
        $pk = $this->getPk();
        if ($this->$pk || (isset($data) && $data[$pk])) {
            $result =  parent::save($data,$options);
        } else {
            $result = $this->add($data,$options);
        }
        return $result;
    }

    protected $_fieldList = [];

    /**
     * @return mixed
     */
    public function getFieldList()
    {
        if (!$this->_fieldList) {
            $this->_fieldList = $this->fields;
        }
        return $this->_fieldList;
    }

    public function fields() {
        return $this->fieldList;
    }

    public function toArray()
    {
        $array = array();
        $fields = $this->fields();

        foreach ($fields as $field)
        {
            if (key_exists($field,$this->data)) {
                $array[$field] = $this->data[$field];
            } else {
                $array[$field] = $this->$field;
            }
        }
        return $array;
    }

    public function toCamelCase($str){

        $array = explode('_', $str);
        $result = '';
        foreach($array as $value){
            $result.= ucfirst($value);
        }

        return $result;
    }


}